package com.yanqu.road.server.manger.config;

import com.yanqu.road.entity.config.player.HouseCreditInfo;
import com.yanqu.road.logic.bussiness.config.HouseBussiness;
import com.yanqu.road.entity.config.player.HouseInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HouseMgr extends TempMgr {

    /**
     * 钱庄信息
     */
    private static Map<Integer, HouseInfo> houseInfoMap;

    /**
     *  钱庄荣誉信息
     */
    private static Map<Integer, HouseCreditInfo> houseCreditInfoMap;

    @Override
    public boolean reloadConfig() {
        houseInfoMap = HouseBussiness.getHouseInfoMap();
        houseCreditInfoMap = HouseBussiness.getHouseCreditDaoImpl();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static HouseInfo getHouseInfo(int houseLv){
        return houseInfoMap.get(houseLv);
    }


    public static HouseCreditInfo getHouseCreditInfo(int houseCreditLv){
        return houseCreditInfoMap.get(houseCreditLv);
    }

    //获取柜台位置花费元宝购买
    public static int getHouseBarCost(int position){
        List<Integer> costList = StringUtils.stringToIntegerList(GameConfig.PATRONS_BANK_BAR_COST,"\\|");
        if(position - 1 < costList.size()){
            return costList.get(position - 1);
        }
        return costList.get(costList.size() - 1);
    }

    //收益单位时间
    public static int getHouseProduceTime(int houseCreditLevel){
        HouseCreditInfo houseCreditInfo = getHouseCreditInfo(houseCreditLevel);
        List<String> produceList = StringUtils.stringToStringList(houseCreditInfo.getProduce(),"-");
        return Integer.parseInt(produceList.get(1));
    }

    //收益最大时间
    public static int getHouseProduceLimitTime(){
        //在配置表里
        return 666;
    }

    public static Map<Integer,Integer> getBankProduceMap(int houseCreditLevel){
        HouseCreditInfo houseCreditInfo = getHouseCreditInfo(houseCreditLevel);
        Map<Integer,Integer> itemRewardMap = new ConcurrentHashMap<>();
        List<String> produceList = StringUtils.stringToStringList(houseCreditInfo.getProduce(),"-");
        List<Integer> rewardList = StringUtils.stringToIntegerList(produceList.get(0),"\\|");
        itemRewardMap.put(GameConfig.PATRONS_BAIYE_EXP_ID,rewardList.get(0));
        itemRewardMap.put(GameConfig.PATRONS_CHOUSUAN_EXP_ID,rewardList.get(1));
        itemRewardMap.put(GameConfig.GAME_ITEM_CREDIT,rewardList.get(2));
        itemRewardMap.put(GameConfig.GAME_ITEM_HOUSE_BANK_LUCK_ID,rewardList.get(3));
        return itemRewardMap;
    }

    //获取钱庄派遣奖励
    public static Property getBankProduceReward(int houseCreditLevel,long rewardTimes){
        Map<Integer,Integer> rewardMap = HouseMgr.getBankProduceMap(houseCreditLevel);
        Property reward = new Property();
        for(Integer goodsId : rewardMap.keySet()){
            reward.addProperty(goodsId, BigInteger.valueOf(rewardMap.get(goodsId)).multiply(BigInteger.valueOf(rewardTimes)));
        }
        return reward;
    }

    //获取金蟾的银两奖励
    public static int getBankProduceSilver(int houseCreditLevel){
        HouseCreditInfo houseCreditInfo = getHouseCreditInfo(houseCreditLevel);
        List<String> produceList = StringUtils.stringToStringList(houseCreditInfo.getProduce(),"-");
        List<Integer> rewardList = StringUtils.stringToIntegerList(produceList.get(0),"\\|");
        return rewardList.get(3);
    }

    //获取财运值最大值
    public static int getLuckSilverMaxValue(){
        List<Integer> paramList = StringUtils.stringToIntegerList(GameConfig.HOUSE_BANK_LUCK_SILVER_COST_LIMIT,"\\|");
        return paramList.get(1);
    }

    //获取财运值使用一次消耗
    public static int getLuckSilverUseValue(){
        List<Integer> paramList = StringUtils.stringToIntegerList(GameConfig.HOUSE_BANK_LUCK_SILVER_COST_LIMIT,"\\|");
        return paramList.get(0);
    }

    //获取随机到的财运位置
    public static int getLuckSilverTime(int pos){
        List<String> paramList = StringUtils.stringToStringList(GameConfig.HOUSE_BANK_LUCK_SILVER,"\\|");
        return StringUtils.stringToIntegerList(paramList.get(pos - 1),":").get(0);
    }

    //随机财运银两奖励
    public static int getRandomLuckSilver() {
        List<String> paramList = StringUtils.stringToStringList(GameConfig.HOUSE_BANK_LUCK_SILVER,"\\|");
        int weight = 0;
        for (String timeWeight : paramList) {
            weight += StringUtils.stringToIntegerList(timeWeight,":").get(1);
        }
        int random = new RandomHelper().next(weight);
        weight = 0;
        for (int i = 0; i < paramList.size(); i++) {
            weight += StringUtils.stringToIntegerList(paramList.get(i),":").get(1);
            if (weight >= random) {
                return i+1;
            }
        }
        return 0;
    }
}
